package com.alibaba.otter.manager.biz.autoconfigure;


import com.alibaba.otter.manager.biz.autoconfigure.properties.ManagerProperties;
import com.alibaba.otter.manager.biz.config.alarm.AlarmRuleService;
import com.alibaba.otter.manager.biz.config.channel.ChannelService;
import com.alibaba.otter.manager.biz.monitor.AlarmRecovery;
import com.alibaba.otter.manager.biz.monitor.Monitor;
import com.alibaba.otter.manager.biz.monitor.impl.GlobalMonitor;
import com.alibaba.otter.manager.biz.monitor.impl.SelfMonitor;
import com.alibaba.otter.shared.arbitrate.ArbitrateManageService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class ManagerMonitorConfiguration {


    private final ManagerProperties managerProperties;

    public ManagerMonitorConfiguration(ManagerProperties managerProperties) {
        this.managerProperties = managerProperties;
    }

    @Bean
    public GlobalMonitor globalMonitor(AlarmRuleService alarmRuleService,
                                       @Qualifier("pipelineMonitor") Monitor pipelineMonitor,
                                       ChannelService channelService,
                                       ArbitrateManageService arbitrateManageService,
                                       AlarmRecovery restartAlarmRecovery) {
        GlobalMonitor globalMonitor = new GlobalMonitor();
        globalMonitor.setnThreads(8);
        globalMonitor.setNeedConcurrent(true);
        globalMonitor.setAlarmRuleService(alarmRuleService);
        globalMonitor.setPipelineMonitor(pipelineMonitor);
        globalMonitor.setChannelService(channelService);
        globalMonitor.setArbitrateManageService(arbitrateManageService);
        globalMonitor.setRestartAlarmRecovery(restartAlarmRecovery);
        return globalMonitor;
    }

    /**
     *
     */
    @Bean
    public SelfMonitor selfMonitor(GlobalMonitor globalMonitor){
        SelfMonitor selfMonitor = new SelfMonitor();
        selfMonitor.setEnable(managerProperties.getMonitor().getSelfEnabled());
        selfMonitor.setInterval(managerProperties.getMonitor().getSelfInterval());
        selfMonitor.setMonitor(globalMonitor);
        return selfMonitor;
    }

}
